﻿//渲染表格数据
layui.use(['table', 'laypage', 'jquery', 'layer'], function () {
    var table = layui.table;
    var laypage = layui.laypage;
    var $ = layui.$;

    var laydate = layui.laydate;
    // 渲染
    laydate.render({
        elem: '#sDateOfBreeding'
    });
    laydate.render({
        elem: '#eDateOfBreeding'
    });
    var pageSize = 15; // 每页显示条数

    //获取列表
    function getListData(pageIndex) {
        //查询条件
        var sDateOfBreeding = $("#sDateOfBreeding").val();
        var eDateOfBreeding = $("#eDateOfBreeding").val();
        var motherEarHorn = $("#motherEarHorn").val();

        //ajax后台交互
        $.ajax({
            url: 'http://10.31.60.18:8000/ProduceGetRout/api/GermlineHistoryRecordsRead/GetGermlineHistoryRecordsList',
            type: 'get',
            dataType: 'json',
            //参数
            data: {
                pageIndex: pageIndex,
                pageSize: pageSize,
                sDateOfBreeding: sDateOfBreeding || "",
                eDateOfBreeding: eDateOfBreeding || "",
                motherEarHorn: motherEarHorn || ""
            },
            success: function (data) {
                table.render({
                    //挂载的表格id
                    elem: '#ID-table-demo-setRowChecked',
                    cols: [[
                        { type: 'checkbox', width: 60, fixed: 'left', unresize: true, align: 'center' },
                        { type: 'numbers', title: '序号', width: 60, fixed: 'center', unresize: true, align: 'center' },
                        { field: 'motherEarHorn', width: 180, title: '种母耳号', sort: true, templet: addLink },
                        { field: 'dateOfBreeding', title: '配种日期', width: 120, align: 'center', templet: function (data) { return data.dateOfBreeding == null ? "" : data.dateOfBreeding.substring(0, 10) } },
                        { field: 'breedEarHorn', title: '种公耳号', width: 120, align: 'center' },
                        { field: 'pregnancyTestDate', title: '妊检日期', width: 120, align: 'center', templet: function (data) { return data.pregnancyTestDate == null ? "" : data.pregnancyTestDate.substring(0, 10) } },
                        { field: 'pregnancyTestResult', title: '妊检结果', width: 120, align: 'center' },
                        { field: 'deliveryDate', title: '分娩日期', width: 120, align: 'center', templet: function (data) { return data.deliveryDate == null ? "" : data.deliveryDate.substring(0, 10) } },
                        { field: 'healthyNumber', title: '健羔数', width: 120, align: 'center' },
                        { field: 'healthyTotalWeight', title: '健羔总重', width: 120, align: 'center' },
                        { field: 'weakLambNumber', title: '弱羔数', width: 120, align: 'center' },
                        { field: 'deformityNumber', title: '畸形数', width: 120, align: 'center' },
                        { field: 'numberOfStillbirths', title: '死胎数', width: 120, align: 'center' },
                        { field: 'mummyNumber', title: '木乃伊数', width: 120, align: 'center' },
                        { field: 'numberOfFreaks', title: '畸胎数', width: 120, align: 'center' },
                        { field: 'weaningDate', title: '断奶日期', width: 120, align: 'center', templet: function (data) { return data.weaningDate == null ? "" : data.weaningDate.substring(0, 10) } },
                        { field: 'weaningNumber', title: '断奶只数', width: 120, align: 'center' },
                        { field: 'weaningLitterWeight', title: '断奶窝重', width: 120, align: 'center' },
                        { field: 'currentParity', title: '当前胎次', width: 120, align: 'center' },
                        { field: 'createBy', title: '创建人', width: 120, align: 'center' },
                        { field: 'createDate', title: '创建时间', width: 120, align: 'center', templet: function (data) { return data.createDate == null ? "" : data.createDate.substring(0, 10) } },
                        { field: 'updateBy', title: '修改人', width: 120, align: 'center' },
                        { field: 'updateDate', title: '修改时间', width: 120, align: 'center', templet: function (data) { return data.updateDate == null ? "" : data.updateDate.substring(0, 10) } },
                        { title: '操作', width: 120, align: 'center', templet: '#barDemo', fixed: 'right', unresize: true },
                    ]],
                    data: data.data.datas
                })
                //分页
                laypage.render({
                    //挂载的分页id
                    elem: 'pagesbox',
                    count: data.data.totalCount,
                    limit: pageSize,
                    limits: [15, 20, 30],
                    curr: pageIndex,
                    layout: ['count', 'prev', 'page', 'next', 'limit', 'skip'],
                    jump: function (obj, first) {
                        if (!first) {
                            pageSize = obj.limit;
                            getListData(obj.curr);
                        }
                    }
                })
            }
        })
    }

    // 单删除
    function deleteData(id) {
        $.ajax({
            url: `http://10.31.60.18:8000/ProduceSetRout/api/GermlineHistoryRecords/RemoveGermlineHistoryRecords?id=${id}`,
            type: 'delete',
            success: function (data) {
                if (data.code == 200) {
                    layer.msg('删除成功');
                    getListData(1);
                } else {
                    layer.msg('删除失败');
                }
            },
            error: function () {
                layer.msg('删除请求失败');
            }
        });
    }

    // 监听工具条，实现删除功能
    table.on('tool(ID-table-demo-setRowChecked)', function (obj) {
        var data = obj.data;
        var id = data.id
        if (obj.event == 'edit') {
            location.href = '/GermlineHistoryRecordsRead/GermlineEdit?id=' + id;
        }

        if (obj.event == 'delete') {
            layer.confirm('确定删除吗？', function (index) {
                deleteData(data.id); // 传递正确的数据项ID
                layer.close(index);
            });
        }
    });

    $('#searchBtn').on('click', function () {
        getListData(1);
    });

    //超链接
    function addLink(d) {
        var addLink = d.motherEarHorn;
        if ('' == addLink || null == addLink || undefined == addLink) {
            return '';
        }
        if (addLink.length > 0) {
            return '<a class="layui-table-link" href="javascript:void(0);" lay-event="link">' + d.motherEarHorn + '</a>';
        }
    }

    // 批量删除
    $(".delAll_btn").click(function () {
        var checkStatus = table.checkStatus('ID-table-demo-setRowChecked'),
            data = checkStatus.data,
            id = "";
        if (data.length > 0) {
            for (var i in data) {
                id += data[i].id + ",";
            }
            id = id.substring(0, id.length - 1);
            console.log(id);
            layer.confirm('确定删除选中的记录？', { icon: 3, title: '提示信息' }, function (index) {
                $.ajax({
                    url: `http://10.31.60.18:8000/ProduceSetRout/api/GermlineHistoryRecords/BatchRemoveGermlineHistoryRecords?ids=${id}`,
                    type: 'delete',
                    success: function (data) {
                        if (data.code == 200) {
                            layer.msg("批量删除成功");
                            getListData(1);
                        } else {
                            layer.msg("批量删除失败");
                        }
                    },
                    error: function () {
                        layer.msg('批量删除请求失败');
                    }
                });
                layer.close(index);
            });
        } else {
            layer.msg("请选择需要删除的记录");
        }
        console.log(checkStatus);
    });

    //新增跳转添加页
    $('#addRowBtn').on('click', function () {
        location.href = '/GermlineHistoryRecordsRead/GermlineAdd'
    })

    //就绪函数
    $(function () {
        // 初始化加载数据
        getListData(1);
    })
})